<template>
  <div class="container">
    <div class="app-container">
      <el-tree default-expand-all :data="depts" :props="defaultProps" :expand-on-click-node="false">
        <!-- 节点结构 -->
        <!-- v-slot="{ node, data }" 只能作用在template -->
        <template v-slot="{ data }">
          <el-row
            style="width: 100%; height: 40px"
            type="flex"
            justify="space-between"
            align="middle"
          >
            <!-- 部门名称 -->
            <el-col :span="20">{{ data.name }}</el-col>
            <!-- 部门管理 -->
            <el-col :span="4" style="display: flex; justify-content: space-around; width: 140px">
              <span class="tree-manager">{{ data.managerName }}</span>
              <el-dropdown @command="operateDept($event, data.id)">
                <span class="el-dropdown-link">
                  操作<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <!-- 下拉菜单选项 -->
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                  <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                  <el-dropdown-item command="del">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </template>
      </el-tree>
    </div>
    <addDept
      ref="addDept"
      :current-node-id="currentNodeId"
      :show-dialog.sync="showDialog"
      @updateDepartment="getDepartment"
    />
  </div>
</template>

<script>
import { getDepartment, delDepartment } from '@/api/department'
import { transListToTreeData } from '@/utils'
import addDept from '@/views/department/components/addDept'

export default {
  name: 'Department',
  components: {
    addDept
  },
  data() {
    return {
      showDialog: false,
      depts: [],
      // 配置树形组件如何解析数据
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      currentNodeId: null // 存储当前点击的id
    }
  },
  created() {
    this.getDepartment()
  },
  methods: {
    operateDept(type, id) {
      if (type === 'add') {
        this.showDialog = true
        this.currentNodeId = id
      } else if (type === 'edit') {
        this.showDialog = true
        this.currentNodeId = id
        // 更新props 异步动作
        // 直接调用子组件的方法 同步的方法
        // 要在子组件获取数据
        // 父组件调用子组件的方法来获取数据
        this.$nextTick(() => {
          this.$refs.addDept.getDepartmentDetail() // this.$refs.addDept等同于子组件的this
        })
      } else {
        this.$confirm('您确认要删除该部门吗', {
          confirmButtonText: '确定',
          cancelButtonText: '取消'
        })
          .then(async() => {
            await delDepartment(id)
            this.$message.success('删除部门成功')
            this.getDepartment()
          })
          .catch(() => {
            this.$message.info('已取消删除')
          })
      }
    },
    async getDepartment() {
      const result = await getDepartment()
      this.depts = transListToTreeData(result, 0)
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  padding: 30px 140px;
  font-size: 14px;

  .tree-manager {
    width: 50px;
    display: inline-block;
    margin-right: 20px;
  }
}
</style>
